<template>
  <div>
    <el-table :data="tableData"
              :header-cell-style="{ background: '#f2f5fc', color: '#555555' }"

    >

      <el-table-column prop="id" v-if="false">
      </el-table-column>
      <el-table-column prop="announcementStatus" label="" width="65">
        <template slot-scope="scope">
          <el-tag
              :type="scope.row.announcementStatus === 0 ? 'info' : (scope.row.announcementStatus === 1 ? 'success' : (scope.row.announcementStatus === 2 ? 'primary' : 'warning'))"
              disable-transitions>{{scope.row.announcementStatus === 0 ? '锁定' :
              (scope.row.announcementStatus === 1 ? '正常' : (scope.row.announcementStatus === 2 ? '加精' : '置顶'))}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" width="350" :formatter="titleFormat">
      </el-table-column>
      <el-table-column prop="userName" label="作者" width="120">
      </el-table-column>
      <el-table-column prop="mainText" label="公告内容" width="450" :formatter="mainTextFormat">
      </el-table-column>
      <el-table-column prop="createTime" label="发布时间" width="150">
      </el-table-column>
      <el-table-column prop="operate" label="">
        <template slot-scope="scope">
          <el-button size="small" type="success" @click="read(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20,30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        title="公告内容"
        :visible.sync="centerDialogVisible"
        width="70%"
        center>

      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-col :span="20">
            <el-input type="textarea" autosize
                      v-model="form.title" :readonly = "true"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="发布者" prop="userName">
          <el-col :span="20">
            <el-input v-model="form.userName" :readonly = "true"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="发布时间" prop="createTime">
          <el-col :span="20">
            <el-input v-model="form.createTime" :readonly = "true"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="内容" prop="mainText">
          <el-col :span="20">
            <el-input type="textarea" autosize
                      v-model="form.mainText" :readonly = "true"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">返  回</el-button>
  </span>
    </el-dialog>
  </div>


</template>

<script>
export default {
  name: "Announcement",
  data() {
    return {
      user:{},
      tableData: [],
      pageSize:10,
      pageNum:1,
      total:0,
      centerDialogVisible:false,
      form:{
        id:'',
        title:'',
        userName:'',
        mainText:'',
        typeId:'',
        createTime:''
      },
      rules: {

      }
    }
  },
  methods:{
    init(){
      this.user = JSON.parse(sessionStorage.getItem('CurUser'))
    },
    mainTextFormat(row, column, cellValue) {
      if (!cellValue) return "";
      if (cellValue.length > 30) {
        //最长固定显示4个字符
        return cellValue.slice(0, 30) + "......";
      }
      return cellValue;
    },
    titleFormat(row, column, cellValue) {
      if (!cellValue) return "";
      if (cellValue.length > 20) {
        //最长固定显示4个字符
        return cellValue.slice(0, 20) + "......";
      }
      return cellValue;
    },
    read(row){
      if(row.announcementStatus == 0){
        this.$message({
          message: '该资源已被锁定 禁止查看！',
          type: 'error'
        });
      }else{
        console.log(row)
        this.centerDialogVisible = true
        this.$nextTick(()=>{
          //赋值到表单
          this.form.id = row.id
          this.form.title = row.title
          this.form.userName = row.userName
          this.form.mainText = row.mainText
          this.form.createTime = row.createTime
        })
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    resetParam(){
      this.typeId = ''
      this.loadPost()
    },
    loadPost(){
      this.$axios.post(this.$httpUrl+'/sns-announcement/listPage',{
        pageSize:this.pageSize,
        pageNum:this.pageNum,
        param:{
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if(res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else{
          alert('获取数据失败')
        }

      })
    }
  },
  beforeMount() {
    this.loadPost()
    this.getTimes()
    this.init()
  }
}
</script>

<style scoped>

</style>